<template>
  <div class="tutorial-detail-page">
    <div class="page-container" v-loading="loading">
      <!-- 返回按钮 -->
      <div class="back-button">
        <el-button @click="$router.back()" :icon="ArrowLeft">
          返回列表
        </el-button>
      </div>

      <!-- 教程详情 -->
      <div class="tutorial-detail" v-if="tutorialDetail">
        <!-- 头部信息 -->
        <div class="detail-header">
          <div class="header-image">
            <img :src="tutorialDetail.coverImage || '/default-tutorial.jpg'" :alt="tutorialDetail.title" />
            <div class="image-overlay">
              <div class="difficulty-badge" :class="`level-${tutorialDetail.difficultyLevel}`">
                {{ getDifficultyText(tutorialDetail.difficultyLevel) }}
              </div>
              <div class="category-badge">
                {{ getCategoryText(tutorialDetail.category) }}
              </div>
              <div class="price-badge" :class="{ 'free': tutorialDetail.isFree }">
                {{ tutorialDetail.isFree ? '免费' : '付费' }}
              </div>
              <div class="recommend-badge" v-if="tutorialDetail.isRecommend">
                <el-icon><Star /></el-icon>
                推荐
              </div>
            </div>
          </div>
          
          <div class="header-content">
            <h1>{{ tutorialDetail.title }}</h1>
            <p class="subtitle" v-if="tutorialDetail.subtitle">{{ tutorialDetail.subtitle }}</p>
            <p class="summary">{{ tutorialDetail.summary }}</p>
            
            <!-- 教程信息 -->
            <div class="tutorial-info">
              <div class="info-grid">
                <div class="info-item">
                  <el-icon><Clock /></el-icon>
                  <div>
                    <span class="label">预计时间</span>
                    <span class="value">{{ tutorialDetail.estimatedTime }} 分钟</span>
                  </div>
                </div>
                <div class="info-item">
                  <el-icon><TrendCharts /></el-icon>
                  <div>
                    <span class="label">难度等级</span>
                    <span class="value">{{ getDifficultyText(tutorialDetail.difficultyLevel) }}</span>
                  </div>
                </div>
              </div>
            </div>

            <!-- 工具材料 -->
            <div class="materials-info" v-if="tutorialDetail.toolsRequired || tutorialDetail.materialsRequired">
              <div class="materials-grid">
                <div class="material-item" v-if="tutorialDetail.toolsRequired">
                  <h4><el-icon><Setting /></el-icon> 所需工具</h4>
                  <p>{{ tutorialDetail.toolsRequired }}</p>
                </div>
                <div class="material-item" v-if="tutorialDetail.materialsRequired">
                  <h4><el-icon><Grid /></el-icon> 所需材料</h4>
                  <p>{{ tutorialDetail.materialsRequired }}</p>
                </div>
              </div>
            </div>

            <!-- 统计信息 -->
            <div class="stats-row">
              <div class="stat-item">
                <el-icon><View /></el-icon>
                <span>{{ tutorialDetail.viewCount }} 浏览</span>
              </div>
              <div class="stat-item">
                <el-icon><StarFilled /></el-icon>
                <span>{{ tutorialDetail.likeCount }} 点赞</span>
              </div>
              <div class="stat-item">
                <el-icon><Collection /></el-icon>
                <span>{{ tutorialDetail.favoriteCount }} 收藏</span>
              </div>
              <div class="stat-item">
                <el-icon><User /></el-icon>
                <span>{{ tutorialDetail.studyCount }} 学习</span>
              </div>
            </div>

            <!-- 操作按钮 -->
            <div class="action-buttons">
              <el-button 
                type="primary"
                size="large"
                :icon="CaretRight"
                @click="startLearning"
                class="start-learn-button"
              >
                开始学习
              </el-button>
              <el-button 
                :icon="StarFilled" 
                @click="handleLike"
                :loading="likeLoading"
                :class="{ 'is-liked': isLiked }"
              >
                {{ isLiked ? '已点赞' : '点赞' }}
              </el-button>
              <el-button 
                :icon="Collection" 
                @click="handleFavorite"
                :loading="favoriteLoading"
                :class="{ 'is-favorited': isFavorited }"
              >
                {{ isFavorited ? '已收藏' : '收藏' }}
              </el-button>
              <el-button 
                :icon="Share" 
                @click="handleShare"
              >
                分享
              </el-button>
            </div>

            <!-- 元信息 -->
            <div class="meta-info">
              <div class="meta-item" v-if="tutorialDetail.creatorName">
                <span class="label">作者:</span>
                <span class="value">{{ tutorialDetail.creatorName }}</span>
              </div>
              <div class="meta-item">
                <span class="label">发布时间:</span>
                <span class="value">{{ formatDate(tutorialDetail.createTime) }}</span>
              </div>
              <div class="meta-item">
                <span class="label">最后更新:</span>
                <span class="value">{{ formatDate(tutorialDetail.updateTime) }}</span>
              </div>
              <div class="meta-item" v-if="tutorialDetail.tags">
                <span class="label">标签:</span>
                <div class="tags">
                  <el-tag 
                    v-for="tag in getTags(tutorialDetail.tags)" 
                    :key="tag" 
                    size="small"
                    @click="searchByTag(tag)"
                  >
                    {{ tag }}
                  </el-tag>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 教程内容 -->
        <div class="detail-content">
          <!-- 简介 -->
          <div class="content-section">
            <h2>教程简介</h2>
            <div class="content-text" v-html="tutorialDetail.content"></div>
          </div>

          <!-- 视频预览 -->
          <div class="video-section" v-if="tutorialDetail.videoUrl">
            <h2>视频预览</h2>
            <div class="video-container">
              <video 
                :src="tutorialDetail.videoUrl" 
                controls 
                :poster="tutorialDetail.coverImage"
                preload="metadata"
              >
                您的浏览器不支持视频播放
              </video>
              <div class="video-info">
                <span v-if="tutorialDetail.videoDuration">
                  时长: {{ Math.floor(tutorialDetail.videoDuration / 60) }}:{{ String(tutorialDetail.videoDuration % 60).padStart(2, '0') }}
                </span>
              </div>
            </div>
          </div>

          <!-- 教程步骤 -->
          <div class="steps-section" v-if="tutorialSteps.length > 0">
            <h2>教程步骤</h2>
            <div class="steps-container">
              <div 
                v-for="(step, index) in tutorialSteps" 
                :key="step.id"
                class="step-item"
              >
                <div class="step-header">
                  <div class="step-number">{{ index + 1 }}</div>
                  <h3>{{ step.title }}</h3>
                </div>
                <div class="step-content">
                  <div class="step-image" v-if="step.imageUrl">
                    <img :src="step.imageUrl" :alt="step.title" />
                  </div>
                  <div class="step-description">
                    <p>{{ step.description }}</p>
                    <div class="step-tips" v-if="step.tips">
                      <h4><el-icon><InfoFilled /></el-icon> 小贴士</h4>
                      <p>{{ step.tips }}</p>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 相关推荐 -->
        <div class="related-section" v-if="relatedList.length > 0">
          <div class="section-header">
            <h2><el-icon><Link /></el-icon> 相关教程</h2>
          </div>
          <div class="related-grid">
            <div
              v-for="item in relatedList"
              :key="item.id"
              class="related-item"
              @click="goToDetail(item.id)"
            >
              <div class="item-image">
                <img :src="item.coverImage || '/default-tutorial.jpg'" :alt="item.title" />
                <div class="difficulty-badge" :class="`level-${item.difficultyLevel}`">
                  {{ getDifficultyText(item.difficultyLevel) }}
                </div>
              </div>
              <div class="item-content">
                <h4>{{ item.title }}</h4>
                <p>{{ item.summary }}</p>
                <div class="item-stats">
                  <span><el-icon><User /></el-icon> {{ item.studyCount }}</span>
                  <span><el-icon><Clock /></el-icon> {{ item.estimatedTime }}min</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 加载失败 -->
      <div class="error-state" v-if="error">
        <el-result
          icon="error"
          title="加载失败"
          sub-title="无法获取教程详情，请稍后重试"
        >
          <template #extra>
            <el-button type="primary" @click="fetchDetail">重新加载</el-button>
            <el-button @click="$router.back()">返回列表</el-button>
          </template>
        </el-result>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { 
  ArrowLeft, Star, Clock, TrendCharts, Setting, Grid,
  View, StarFilled, Collection, User, Share, CaretRight, Link, InfoFilled
} from '@element-plus/icons-vue'
import { tutorialApi, tutorialStepApi } from '@/api'
import { useUserStore } from '@/stores/user'
import type { Tutorial, TutorialStep } from '@/types/common'
import { TUTORIAL_CATEGORIES, TUTORIAL_DIFFICULTY } from '@/types/common'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

// 响应式数据
const loading = ref(true)
const error = ref(false)
const tutorialDetail = ref<any | null>(null) // 使用 any 类型因为后端返回的是 Map<String, Object>
const tutorialSteps = ref<TutorialStep[]>([])
const relatedList = ref<Tutorial[]>([])
const interactionStatus = ref<any>({})

// 交互状态
const likeLoading = ref(false)
const favoriteLoading = ref(false)
const isLiked = computed(() => interactionStatus.value.liked || false)
const isFavorited = computed(() => interactionStatus.value.favorited || false)

// 获取详情ID
const detailId = computed(() => Number(route.params.id))

// 获取教程详情
const fetchDetail = async () => {
  if (!detailId.value) return

  loading.value = true
  error.value = false

  try {
    // 并发请求详情和步骤
    const [detailResult, stepsResult] = await Promise.allSettled([
      tutorialApi.getTutorialDetail(detailId.value),
      tutorialStepApi.getTutorialSteps(detailId.value)
    ])

    // 处理详情数据
    if (detailResult.status === 'fulfilled') {
      tutorialDetail.value = detailResult.value.data.tutorial
      // 如果后端返回的步骤数据在详情中，也要处理
      if (detailResult.value.data.steps) {
        tutorialSteps.value = detailResult.value.data.steps
      }
    } else {
      throw new Error('获取教程详情失败')
    }

    // 处理步骤数据 - 优先使用详情API中的步骤数据
    if (!tutorialSteps.value || tutorialSteps.value.length === 0) {
      if (stepsResult.status === 'fulfilled') {
        tutorialSteps.value = stepsResult.value.data
      } else {
        tutorialSteps.value = []
      }
    }

    // 获取相关教程
    try {
      // TODO: 实现获取相关教程的 API
      // const relatedResponse = await tutorialApi.getRelatedTutorials(detailId.value)
      // relatedList.value = relatedResponse.data
    } catch (err) {
      // 静默处理错误
    }

    // 如果用户已登录，获取交互状态
    if (userStore.isLoggedIn) {
      try {
        const statusResponse = await tutorialApi.getTutorialInteraction(detailId.value)
        interactionStatus.value = statusResponse.data
      } catch (err) {
        // 静默处理错误
      }
    }

    // 增加浏览量
    try {
      await tutorialApi.increaseViewCount(detailId.value)
    } catch (err) {
      // 静默处理错误
    }

  } catch (err) {
    error.value = true
    ElMessage.error('获取教程详情失败')
  } finally {
    loading.value = false
  }
}


// 点赞操作
const handleLike = async () => {
  if (!userStore.isLoggedIn || !userStore.token) {
    ElMessage.warning('请先登录')
    router.push('/login')
    return
  }

  // 防止重复点击
  if (likeLoading.value) {
    return
  }

  likeLoading.value = true
  const originalStatus = isLiked.value
  const originalCount = tutorialDetail.value?.likeCount || 0
  
  try {
    const response = await tutorialApi.likeTutorial(detailId.value)
    const responseData = (response as any).data
    const newIsLiked = responseData.isLiked || responseData.liked
    const newLikeCount = responseData.likeCount
    const message = responseData.message

    // 更新本地状态
    interactionStatus.value.liked = newIsLiked
    interactionStatus.value.isLiked = newIsLiked
    if (tutorialDetail.value) {
      tutorialDetail.value.likeCount = newLikeCount
    }

    // 显示后端返回的消息，或默认消息
    ElMessage.success(message || (newIsLiked ? '点赞成功' : '取消点赞成功'))
    
  } catch (error: any) {
    // 发生错误时恢复原始状态
    interactionStatus.value.liked = originalStatus
    interactionStatus.value.isLiked = originalStatus
    if (tutorialDetail.value) {
      tutorialDetail.value.likeCount = originalCount
    }
    
    // 检查是否是重复点赞的友好提示
    if (error.message && error.message.includes('您已经点赞过')) {
      ElMessage.warning('您已经点赞过该内容')
      // 刷新当前状态
      await refreshInteractionStatus()
      return
    }
    
    if (error.response?.status === 400) {
      const errorMsg = error.response?.data?.message || error.message
      if (errorMsg.includes('您已经点赞过') || errorMsg.includes('已点赞')) {
        ElMessage.warning('您已经点赞过该内容')
        await refreshInteractionStatus()
      } else {
        ElMessage.error('请求参数错误，请检查登录状态')
        userStore.logout()
        router.push('/login')
      }
    } else if (error.response?.status === 401) {
      ElMessage.error('登录已过期，请重新登录')
      userStore.logout()
      router.push('/login')
    } else {
      ElMessage.error('操作失败，请稍后重试')
    }
  } finally {
    // 添加短暂延迟，避免用户快速点击
    setTimeout(() => {
      likeLoading.value = false
    }, 300)
  }
}

// 刷新互动状态
const refreshInteractionStatus = async () => {
  if (!userStore.isLoggedIn) return
  
  try {
    const statusResponse = await tutorialApi.getTutorialInteraction(detailId.value)
    interactionStatus.value = statusResponse.data
  } catch (err) {
    // 静默处理错误
  }
}

// 收藏操作
const handleFavorite = async () => {
  if (!userStore.isLoggedIn || !userStore.token) {
    ElMessage.warning('请先登录')
    router.push('/login')
    return
  }

  favoriteLoading.value = true
  try {
    const response = await tutorialApi.favoriteTutorial(detailId.value)
    const responseData = (response as any).data
    const isFavorited = responseData.isFavorited || responseData.favorited
    const newFavoriteCount = responseData.favoriteCount

    // 更新本地状态
    interactionStatus.value.favorited = isFavorited
    interactionStatus.value.isFavorited = isFavorited
    if (tutorialDetail.value) {
      tutorialDetail.value.favoriteCount = newFavoriteCount
    }

    ElMessage.success(isFavorited ? '收藏成功' : '取消收藏成功')
  } catch (error: any) {
    if (error.response?.status === 400) {
      ElMessage.error('请求参数错误，请检查登录状态')
      // 如果400错误，可能是认证问题，尝试重新登录
      userStore.logout()
      router.push('/login')
    } else if (error.response?.status === 401) {
      ElMessage.error('登录已过期，请重新登录')
      userStore.logout()
      router.push('/login')
    } else {
      ElMessage.error('操作失败，请稍后重试')
    }
  } finally {
    favoriteLoading.value = false
  }
}

// 开始学习
const startLearning = () => {
  if (!userStore.isLoggedIn) {
    ElMessage.warning('请先登录后再开始学习')
    router.push({
      path: '/login',
      query: { redirect: `/tutorial/${detailId.value}/study` }
    })
    return
  }
  
  router.push(`/tutorial/${detailId.value}/study`)
}

// 分享操作
const handleShare = async () => {
  try {
    if (navigator.share) {
      await navigator.share({
        title: tutorialDetail.value?.title,
        text: tutorialDetail.value?.summary,
        url: window.location.href
      })
    } else {
      await navigator.clipboard.writeText(window.location.href)
      ElMessage.success('链接已复制到剪贴板')
    }

    if (tutorialDetail.value) {
      tutorialDetail.value.shareCount++
    }
  } catch (error) {
    if (error instanceof Error && error.name !== 'AbortError') {
      ElMessage.error('分享失败')
    }
  }
}

// 跳转到详情页
const goToDetail = (id: number) => {
  router.push(`/tutorial/${id}`)
}

// 按标签搜索
const searchByTag = (tag: string) => {
  router.push({
    path: '/search',
    query: { keyword: tag, tab: 'tutorial' }
  })
}

// 获取分类文本
const getCategoryText = (category: number) => {
  return TUTORIAL_CATEGORIES[category as keyof typeof TUTORIAL_CATEGORIES] || '其他'
}

// 获取难度文本
const getDifficultyText = (level: number) => {
  return TUTORIAL_DIFFICULTY[level as keyof typeof TUTORIAL_DIFFICULTY] || '未知'
}

// 获取标签数组
const getTags = (tagsString: string) => {
  return tagsString ? tagsString.split(',').filter(tag => tag.trim()) : []
}

// 格式化日期
const formatDate = (dateString: string) => {
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN')
}

onMounted(() => {
  fetchDetail()
})

// 监听路由变化，重新获取详情
watch(() => route.params.id, (newId) => {
  if (newId && newId !== String(detailId.value)) {
    fetchDetail()
  }
})
</script>

<style scoped>
.tutorial-detail-page {
  min-height: 100vh;
  background: #f5f5f5;
}

.page-container {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
}

.back-button {
  margin-bottom: 20px;
}

.tutorial-detail {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

/* 头部信息 */
.detail-header {
  position: relative;
}

.header-image {
  position: relative;
  height: 300px;
  overflow: hidden;
}

.header-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.image-overlay {
  position: absolute;
  top: 20px;
  left: 20px;
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.difficulty-badge {
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: bold;
  color: #fff;
}

.difficulty-badge.level-1 {
  background: #4caf50;
}

.difficulty-badge.level-2 {
  background: #ff9800;
}

.difficulty-badge.level-3 {
  background: #f44336;
}

.category-badge {
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: bold;
}

.price-badge {
  color: #fff;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: bold;
  background: #ff9800;
}

.price-badge.free {
  background: #4caf50;
}

.recommend-badge {
  background: #43a047;
  color: #fff;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: bold;
  display: flex;
  align-items: center;
  gap: 4px;
}

.header-content {
  padding: 32px;
}

.header-content h1 {
  font-size: 32px;
  font-weight: bold;
  color: #333;
  margin-bottom: 12px;
  line-height: 1.3;
}

.subtitle {
  font-size: 18px;
  color: #666;
  margin-bottom: 16px;
  font-weight: 500;
}

.summary {
  font-size: 16px;
  color: #666;
  line-height: 1.6;
  margin-bottom: 24px;
}

/* 教程信息 */
.tutorial-info {
  margin-bottom: 24px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  background: #f9f9f9;
  border-radius: 8px;
}

.info-item .el-icon {
  font-size: 24px;
  color: #43a047;
}

.info-item .label {
  display: block;
  font-size: 12px;
  color: #999;
  margin-bottom: 4px;
}

.info-item .value {
  display: block;
  font-size: 14px;
  color: #333;
  font-weight: bold;
}

/* 材料信息 */
.materials-info {
  margin-bottom: 24px;
}

.materials-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 16px;
}

.material-item {
  padding: 20px;
  background: #f0f8ff;
  border-radius: 8px;
  border: 1px solid #e3f2fd;
}

.material-item h4 {
  font-size: 16px;
  color: #333;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.material-item h4 .el-icon {
  color: #43a047;
}

.material-item p {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
}

.stats-row {
  display: flex;
  gap: 24px;
  margin-bottom: 24px;
  padding: 16px 0;
  border-top: 1px solid #f0f0f0;
  border-bottom: 1px solid #f0f0f0;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: #666;
}

.stat-item .el-icon {
  font-size: 16px;
}

.action-buttons {
  display: flex;
  gap: 16px;
  margin-bottom: 32px;
  flex-wrap: wrap;
}

.start-learn-button {
  font-size: 16px;
  padding: 12px 32px;
  font-weight: bold;
  background: #d32f2f;
  border-color: #d32f2f;
}

.start-learn-button:hover {
  background: #b71c1c;
  border-color: #b71c1c;
}

.action-buttons .el-button.is-liked {
  background: #f56c6c;
  border-color: #f56c6c;
  color: #fff;
}

.action-buttons .el-button.is-favorited {
  background: #e6a23c;
  border-color: #e6a23c;
  color: #fff;
}

.meta-info {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

.meta-item {
  display: flex;
  align-items: center;
  font-size: 14px;
}

.meta-item .label {
  color: #999;
  margin-right: 8px;
  min-width: 80px;
}

.meta-item .value {
  color: #333;
}

.tags {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.tags .el-tag {
  cursor: pointer;
  transition: all 0.3s;
}

.tags .el-tag:hover {
  background: #43a047;
  color: #fff;
  border-color: #43a047;
}

/* 详细内容 */
.detail-content {
  padding: 32px;
  border-top: 1px solid #f0f0f0;
}

.content-section,
.video-section,
.steps-section {
  margin-bottom: 40px;
}

.content-section h2,
.video-section h2,
.steps-section h2 {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 2px solid #43a047;
  display: inline-block;
}

.content-text {
  font-size: 16px;
  line-height: 1.8;
  color: #333;
}

/* 视频区域 */
.video-container {
  position: relative;
  background: #000;
  border-radius: 8px;
  overflow: hidden;
}

.video-container video {
  width: 100%;
  height: auto;
  max-height: 400px;
}

.video-info {
  position: absolute;
  bottom: 10px;
  right: 10px;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
}

/* 步骤区域 */
.steps-container {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.step-item {
  border: 1px solid #f0f0f0;
  border-radius: 12px;
  overflow: hidden;
  transition: all 0.3s;
}

.step-item:hover {
  border-color: #43a047;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.step-header {
  background: #f9f9f9;
  padding: 16px 20px;
  display: flex;
  align-items: center;
  gap: 16px;
}

.step-number {
  width: 40px;
  height: 40px;
  background: #43a047;
  color: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: bold;
}

.step-header h3 {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  margin: 0;
}

.step-content {
  padding: 20px;
  display: flex;
  gap: 20px;
}

.step-image {
  flex-shrink: 0;
  width: 200px;
}

.step-image img {
  width: 100%;
  height: auto;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.step-description {
  flex: 1;
}

.step-description p {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
  margin-bottom: 16px;
}

.step-tips {
  background: #fff9c4;
  border: 1px solid #ffd54f;
  border-radius: 8px;
  padding: 16px;
}

.step-tips h4 {
  font-size: 14px;
  color: #f57c00;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.step-tips p {
  font-size: 14px;
  color: #666;
  margin: 0;
}

/* 相关推荐 */
.related-section {
  padding: 32px;
  border-top: 1px solid #f0f0f0;
  background: #fafafa;
}

.section-header h2 {
  font-size: 20px;
  font-weight: bold;
  color: #333;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.related-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

.related-item {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.related-item:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.item-image {
  position: relative;
  height: 120px;
  overflow: hidden;
}

.item-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.related-item:hover .item-image img {
  transform: scale(1.05);
}

.item-content {
  padding: 16px;
}

.item-content h4 {
  font-size: 14px;
  font-weight: bold;
  color: #333;
  margin-bottom: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.item-content p {
  font-size: 12px;
  color: #666;
  line-height: 1.4;
  margin-bottom: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.item-stats {
  display: flex;
  gap: 12px;
  font-size: 12px;
  color: #999;
}

.item-stats span {
  display: flex;
  align-items: center;
  gap: 2px;
}

.error-state {
  padding: 40px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-container {
    padding: 16px;
  }

  .header-content {
    padding: 24px 20px;
  }

  .header-content h1 {
    font-size: 24px;
  }

  .info-grid {
    grid-template-columns: 1fr;
  }

  .materials-grid {
    grid-template-columns: 1fr;
  }

  .stats-row {
    flex-wrap: wrap;
    gap: 16px;
  }

  .action-buttons {
    flex-wrap: wrap;
  }

  .meta-info {
    grid-template-columns: 1fr;
  }

  .detail-content {
    padding: 24px 20px;
  }

  .step-content {
    flex-direction: column;
  }

  .step-image {
    width: 100%;
  }

  .related-section {
    padding: 24px 20px;
  }

  .related-grid {
    grid-template-columns: 1fr;
  }
}
</style>
